<template>
  <div class="art-pub-container">
    <el-form :model="addForm" :rules="rules" label-width="100px" ref="addRef">
      <el-form-item label="文章标题" placeholder="请填写文章标题" prop="title">
        <el-input  maxlength="30" v-model="addForm.title"></el-input>
      </el-form-item>
      <el-form-item label="文章分类" prop="cate_id">
        <el-select placeholder="请选择分类" clearable v-model="addForm.cate_id">
            <el-option v-for="item in cateList" :key="item.id" :label="item.cate_name" :value="item.id"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文章内容">
        <!-- 富文本编辑器组件 -->
        <quill-editor v-model="addForm.content"></quill-editor>
      </el-form-item>
      <el-form-item label="文章封面" >
        <div class="cover-box">
          <img :src="addForm.cover_img" alt="" class="cover-img" />
          <el-link type="primary" :underline="false" @click="$refs.iptFile.click()">+ 选择封面</el-link>
          <input type="file" ref="iptFile" accept="image/*" hidden @change="previewImg">
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="publish('已发布')">发布文章</el-button>
        <el-button type="info"  @click="publish('草稿')">存为草稿</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import defaultCover from '@/assets/images/cover.jpg'
import { reqPublishArticle } from '@/api/article'
export default {
  name: 'ArtPub',
  computed: {
    ...mapState('article', ['cateList'])
  },
  data () {
    return {
      // 发布文章提交的对象
      addForm: {
        title: '', // 文章标题
        cate_id: '', // 文章分类id
        content: '', // 文章内容
        state: '', // 文章发布状态
        cover_img: defaultCover // 文章封面图

      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: ['blur', 'change'] },
          { min: 1, max: 30, message: '文章标题的长度为1-30个字符', trigger: ['blur', 'change'] }
        ],
        cate_id: [{ required: true, message: '请选择文章分类', trigger: ['blur', 'change'] }]
      }

    }
  },
  methods: {
    // 本地预览
    previewImg (e) {
      // console.log('选择文件了')
      // console.log(e.target.files[0])
      const file = e.target.files[0]
      // console.log(file)
      const url = URL.createObjectURL(file)
      console.log(url)
      this.addForm.cover_img = url
      this.file = file
    },
    publish (str) {
      // console.log('发布文章')
      // console.log(str)
      this.addForm.state = str
      console.log(this.addForm)
      this.$refs.addRef.validate(async boo => {
        // console.log(boo)
        if (boo) {
          const fm = new FormData()
          fm.append('title', this.addForm.title)
          fm.append('cate_id', this.addForm.cate_id)
          fm.append('content', this.addForm.content)
          fm.append('state', this.addForm.state)
          // cover_img要的是一个文件对象
          fm.append('cover_img', this.file)
          const { data } = await reqPublishArticle(fm)
          if (data.code !== 0) return this.$message.error(data.message)
          this.$message.success(data.message)
          this.$emit('closeDialog')
        }
      })
    }
  }

}
</script>

<style lang="less" scoped>
.art-pub-container {
  padding: 0 30px 20px 0px;
}

.el-select {
  width: 100%;
}

/deep/ .ql-snow .ql-color-picker .ql-picker-label svg,
/deep/ .ql-snow .ql-icon-picker .ql-picker-label svg {
  position: relative;
  top: -6px;
}

/deep/ .ql-snow .ql-picker-label::before {
  position: relative;
  top: -8px;
}

/deep/ .ql-editor {
  min-height: 300px;
}

.cover-img {
  object-fit: cover;
  width: 400px;
  height: 280px;
  margin-right: 10px;
}

.cover-box {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
</style>
